package com.example.lsrsbackend.service.impl;

import com.example.lsrsbackend.mapper.StudentMapper;
import com.example.lsrsbackend.model.Student;
import com.example.lsrsbackend.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public boolean login(Student student) {
        Student dbStudent = studentMapper.findByUsernameAndPassword(student.getUsername(), student.getPassword());
        return dbStudent != null;
    }

    // 实现接口中的其他方法
    public void register(Student student) {
        studentMapper.insertStudent(student);
    }
    //修改密码
    @Override
    public boolean changePassword(String username, String newPassword){
        try {
            // 在实际应用中，这里应该对新密码进行加密
            // String encryptedPassword = passwordEncoder.encode(newPassword);

            int result = studentMapper.updatePasswordByUsername(username, newPassword);
            return result > 0;
        } catch (Exception e) {
            // 记录日志
            System.err.println("修改密码失败: " + e.getMessage());
            return false;
        }
    }
    //根据username查找全部数据
    @Override
    public Student getStudentInfo(String username) {
        return studentMapper.findByUsername(username);
    }
    //部分更新策略
    @Override
    public boolean partialupdate(String username, Map<String, Object> updates) {
        try{
            //1 查询原学生信息
            Student student = getStudentInfo( username);
            if(student ==  null){
                return false;
            }

            //2 应用更新字段
            applyUpdateToStudent(student,updates);

            //3 执行更新
            int result = studentMapper.updateStudentInfo(student);
            return result > 0;
        }catch (Exception e){
            System.err.println("部分更新失败: " + e.getMessage());
            return false;
        }
    }

    /**
     * 将更新应用到学生对象
     */

    private void applyUpdateToStudent(Student student,Map<String , Object> updates){
        for(Map.Entry<String,Object> entry : updates.entrySet()){
            String field = entry.getKey();
            Object value = entry.getValue();

            switch (field) {
                case "grade":
                    if (value instanceof String) {
                        student.setGrade((String) value);
                    }
                    break;
                case "education":
                    if (value instanceof String) {
                        student.setEducation((String) value);
                    }
                    break;
                case "college":
                    if (value instanceof String) {
                        student.setCollege((String) value);
                    }
                    break;
                case "major":
                    if (value instanceof String) {
                        student.setMajor((String) value);
                    }
                    break;
                case "phone":
                    if (value instanceof String) {
                        student.setPhone((String) value);
                    }
                    break;
                case "email":
                    if (value instanceof String) {
                        student.setEmail((String) value);
                    }
                    break;
                default:
                    // 忽略不支持的字段
                    System.out.println("忽略不支持更新的字段: " + field);
                    break;
            }
        }
    }

    //获取全部students信息
    public List<Student> getAllStudents() {
        return studentMapper.getAllStudents();
    }

    @Override
    @Transactional
    public boolean deleteStudent(String username) {
        try {
            // 先检查用户是否存在
            Student student = studentMapper.findByUsername(username);
            if (student == null) {
                System.err.println("删除失败：用户不存在 - " + username);
                return false;
            }

            // 执行删除操作
            int result = studentMapper.deleteByUsername(username);
            if (result > 0) {
                System.out.println("成功删除用户: " + username);
                return true;
            } else {
                System.err.println("删除用户失败: " + username);
                return false;
            }
        } catch (Exception e) {
            System.err.println("删除用户异常: " + e.getMessage());
            return false;
        }
    }

    public int freezeStudent(String username) {
        return studentMapper.freezeByUsername(username);
    }

    public int unfreezeStudent(String username) {
        return studentMapper.unfreezeByUsername(username);
    }

    public String getStudentStatus(String username) {
        return studentMapper.selectStatusByUsername(username);
    }
}
